package com.pxmeta.app.task;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.google.common.collect.Lists;
import com.pxmeta.common.utils.StringUtils;
import com.pxmeta.common.utils.agora.AgoraClient;
import com.pxmeta.dynamic.service.RedisLockService;
import com.pxmeta.party.entity.Association;
import com.pxmeta.party.entity.Party;
import com.pxmeta.party.entity.vo.ChannelUserVO;
import com.pxmeta.party.service.IAssociationService;
import com.pxmeta.party.service.IPartyService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;

@Component
@RequiredArgsConstructor
@Slf4j
public class PartyTask {

    private final IAssociationService associationService;

    private final IPartyService partyService;

    private final AgoraClient agoraClient;

    private final RedisLockService redisLockService;


//    /**
//     * 房间自动隐藏
//     * 每50分钟触发一次
//     */
//    @Scheduled(cron = "0 0/50 * * * ?")
//    @Transactional(rollbackFor = RuntimeException.class)
//    public void partyAutomaticHidden(){
//        String lock = redisLockService.lock("task_partyAutomaticHidden", 1000, 200);
//
//        if (StringUtils.isNotBlank(lock)){
//            List<Association> associations = associationService.list();
//            List<String> partyIds = Lists.newLinkedList();
//            for (Association association : associations) {
//                String partyId = association.getPartyIds();
//                if (StringUtils.isNotBlank(partyId)){
//                    List<String> list = Arrays.asList(partyId.split(","));
//                    partyIds.addAll(list);
//                }
//            }
//            if (partyIds.isEmpty()){
//                return;
//            }
//
//            LambdaQueryWrapper<Party> wrapper = Wrappers.lambdaQuery();
//            wrapper.in(Party::getId, partyIds)
//                    .eq(Party::getIsHidden,Boolean.FALSE);
//            List<Party> partyList = partyService.list(wrapper);
//            List<Party> updateList = Lists.newLinkedList();
//
//            for (Party party : partyList) {
//                try {
//                    ChannelUserVO userVo = agoraClient.getUsers(party.getId()).toJavaObject(ChannelUserVO.class);
//                    List<Integer> usernames = userVo.getBroadcasters();
//                    if (CollectionUtil.isEmpty(usernames)){
//                        party.setIsHidden(Boolean.TRUE);
//                        updateList.add(party);
//                    }
//                }catch (Exception ignored){
//
//                }
//
//            }
//
//            if (!updateList.isEmpty()){
//                partyService.updateBatchById(updateList);
//            }
//        }
//    }
}
